# -*- coding: utf-8 -*-

import json, struct, gzip, time, random, hashlib, uuid
#from urllib import requests
import requests
from io import BytesIO

# https://upl.baidu-int.com/hotwords_ctx
# https://audiotest.baidu.com/hotwords_ctx_tst
g_host = "https://upl.baidu-int.com/hotwords_ctx"

def generate_sign(username):
    pass

def get_md5_value(str):
    my_md5 = hashlib.md5()
    my_md5.update(str.encode('utf8'))
    my_md5_Digest = my_md5.hexdigest()
    return my_md5_Digest

def gzip_compress(buf):
    out = BytesIO()
    with gzip.GzipFile(fileobj=out, mode="w") as f:
        f.write(buf.encode())
    return out.getvalue()


def request_model_train_hotwords(pid, delete=False):
    slot = {}
    slot["slot_name"] = "hotwords"
    slot["version"] = 1
    slot["mode"] = 0
    slot["hot_words_type"] = "HOT_WORDS_TYPE_DEFAULT"
    slot_value = []
    with open('hotwords.txt', encoding='utf-8') as file:
        # 读取文件
        while True:
            contents = file.readline().replace('\r','').replace('\n','')
            if not contents:
                break
            elif contents.startswith("#"):
                continue
            else:
                print("contents = ", contents)
                slot_value.append(contents)
    # slot_value.append("巴博斯模式")
    # slot_value.append("精灵3号的价格")
    # slot_value.append("荔枝苹果酒")
    # slot_value.append("荔枝草莓九")
    # slot_value.append("我想去大气车")
    slot["hot_words"] = slot_value
    slot_string = json.dumps(slot)
    body = "glb=" + str(uuid.uuid1())
    body = body + "&upload_pid=" + str(pid)
    #body = body + "&uid=" + cuid
    body = body + "&sign=" + get_md5_value(str(pid) + "ABCD" + slot_string)
    body = body + "&content=" + slot_string
    if delete:
        body = body + "&delete=1"

    response = requests.post(g_host, data=body)
    print(body)
    print(response.content)



if __name__ == "__main__":
    # smart 试驾 1207 ；正式 1190
    # lotus 11142
    # G836 845
    # 测试 1170203
    request_model_train_hotwords(11142)


